package com.hk.yajin.function.members.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.developmentlib.utills.DeviceUtils;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.base.MyApplication;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.model.UserInfo;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;
import com.umeng.message.PushAgent;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterSmsCodeActivity extends BaseActivity {

    public static final String ACOUNT = "acount";
    public static final String PASSWORD = "password";
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_submit_code)
    TextView tvSubmitCode;
    @BindView(R.id.et_invite)
    TextView etInvite;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms_code);
        ButterKnife.bind(this);

        intiView();
    }

    private void intiView() {
        initToolBar(R.string.sms_code_title);
    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(RegisterSmsCodeActivity.this).setTitle("提示")//设置对话框标题
                .setMessage("注册尚未完成，确定现在退出吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }


    private void submitSmsCode() {
        String code = etCode.getText().toString();
        if (TextUtils.isEmpty(code)) {
            showShortToast(getString(R.string.please_input_code));
            return;
        }

        if (isNetWorkConnect()) {
            showLoadingView();
            final String account = getIntent().getStringExtra(ACOUNT);
            final String pwd = getIntent().getStringExtra(PASSWORD);
            Map<String, Object> reqParam = new HashMap<String, Object>();
            reqParam.put("mobile", account);
            reqParam.put("password", pwd);
            reqParam.put("code", code);
            reqParam.put("device_token", PushAgent.getInstance(MyApplication.getInstance()).getRegistrationId());
            if (!TextUtils.isEmpty(etInvite.getText().toString().trim())) {
                reqParam.put("inviter", etInvite.getText().toString().trim());
            }
            HttpRequestUtils.post(false, UrlConstants.FUNCTION_REGISTER, reqParam, false, new HttpCallBack() {

                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    UserInfo userInfo = FastjsonUtils.getBeanObject(data, UserInfo.class);
                    UserInfoManager.getInstance().loginSucceed(account, pwd, userInfo);
                    startActivity(new Intent(RegisterSmsCodeActivity.this, RegisterUserNameActivity.class));
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    showShortToast(bean.getMsg());
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    showShortToast(tip);
                }

            });
        }
    }

    @OnClick(R.id.tv_submit_code)
    public void onViewClicked() {
        submitSmsCode();
    }
}
